package org.jeecg.modules.htxincailiao.industry.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.entity.HTPlatformUser;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.industry.domain.bo.HTIndustryApplyBO;
import org.jeecg.modules.htxincailiao.industry.domain.dto.HTApplyInfoDTO;
import org.jeecg.modules.htxincailiao.industry.domain.dto.HTIndustrySalonApplyDTO;
import org.jeecg.modules.htxincailiao.industry.entity.HTIndustrySalonApply;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustrySalonApplyService;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustrySalonCheckService;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustrySalonService;
import org.jeecg.modules.htxincailiao.order.service.IHTOrderService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Arrays;

/**
 * @Description: ht_industry_salon_apply
 * @Author: jeecg-boot
 * @Date: 2021-06-05
 * @Version: V1.0
 */
@Api(tags = "ht_industry_salon_apply")
@RestController
@RequestMapping("/industry/hTIndustrySalonApply")
@Slf4j
public class HTIndustrySalonApplyController extends JeecgController<HTIndustrySalonApply, IHTIndustrySalonApplyService> {
	@Resource
	private IHTIndustrySalonApplyService hTIndustrySalonApplyService;

	@Resource
	private IHTIndustrySalonService industrySalonService;

	@Resource
	private IHTIndustrySalonCheckService industrySalonCheckService;

	@Resource
	private IHTOrderService orderService;

	@Resource
	private IHTAccountService accountService;

	/**
	 * 分页列表查询
	 *
	 * @param hTIndustrySalonApply
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-分页列表查询")
	@ApiOperation(value = "ht_industry_salon_apply-分页列表查询", notes = "ht_industry_salon_apply-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HTIndustrySalonApply hTIndustrySalonApply,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HTIndustrySalonApply> queryWrapper = QueryGenerator.initQueryWrapper(hTIndustrySalonApply, req.getParameterMap());
		Page<HTIndustrySalonApply> page = new Page<HTIndustrySalonApply>(pageNo, pageSize);
		IPage<HTIndustrySalonApply> pageList = hTIndustrySalonApplyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param hTIndustrySalonApply
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-添加")
	@ApiOperation(value = "ht_industry_salon_apply-添加", notes = "ht_industry_salon_apply-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HTIndustrySalonApply hTIndustrySalonApply) {
		hTIndustrySalonApplyService.save(hTIndustrySalonApply);
		return Result.OK("添加成功！");
	}

	/**
	 * 添加
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-添加")
	@ApiOperation(value = "ht_industry_salon_apply-添加", notes = "ht_industry_salon_apply-添加")
	@PostMapping(value = "/userApply")
	@Transactional
	public Result<?> userApply(HttpServletRequest request, @Valid @RequestBody HTIndustrySalonApplyDTO dto) {
		Result<JSONObject> result = new Result<>();

		if (dto.getApplyObjectType() == 0 && oConvertUtils.isEmpty(dto.getEnterpriseName())) {
			return result.error500("企业名不能为空");
		}

		String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		String username = JwtUtil.getUsername(token);
		HTPlatformUser user = accountService.queryFrontendUserByUsername(username);

		if (oConvertUtils.isNotEmpty(user)) {
			//同一个会议同一个用户只能报一次
			HTIndustrySalonApply hasAppliedSalon = hTIndustrySalonApplyService.queryBy(user.getId(), dto.getSalonId(), dto.getApplyObjectType());
			if (oConvertUtils.isNotEmpty(hasAppliedSalon)) {
				return Result.error("不能重复报名");
			}

//			System.out.printf("\n\n\n userApply %s \n\n\n", JSONObject.toJSONString(dto));

			HTIndustryApplyBO bo = hTIndustrySalonApplyService.handleUserApply(dto, user.getId());
			result.setSuccess(bo.isSuccess());
			if (bo.isSuccess()) {
				if (oConvertUtils.isNotEmpty(bo.getPaymentItem())) {
					JSONObject obj = new JSONObject();
					obj.put("paymentInfo", bo.getPaymentItem());
					result.setResult(obj);
				}
			} else {
				result.error500("报名失败，请稍后尝试");
			}
		} else {
			result.error500("请先登录");
		}
		return result;
	}

	/**
	 * 添加
	 *
	 * @param dto
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-添加")
	@ApiOperation(value = "ht_industry_salon_apply-添加", notes = "ht_industry_salon_apply-添加")
	@PostMapping(value = "/getUserApplyInfo")
	@Transactional
	public Result<?> getUserApplyInfo(HttpServletRequest request, @Valid @RequestBody HTApplyInfoDTO dto) {
		Result<JSONObject> result = new Result<>();

		String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		String username = JwtUtil.getUsername(token);
		HTPlatformUser user = accountService.queryFrontendUserByUsername(username);

		if (oConvertUtils.isNotEmpty(user)) {
			HTIndustrySalonApply hasAppliedSalon = hTIndustrySalonApplyService.queryBy(user.getId(), dto.getSalonId(), dto.getApplyObjectType());
			result.setResult(JSON.parseObject(JSONObject.toJSONString(hasAppliedSalon)));
		} else {
			result.error500("请先登录");
		}

		return result;
	}


	/**
	 * 编辑
	 *
	 * @param hTIndustrySalonApply
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-编辑")
	@ApiOperation(value = "ht_industry_salon_apply-编辑", notes = "ht_industry_salon_apply-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HTIndustrySalonApply hTIndustrySalonApply) {
		hTIndustrySalonApplyService.updateById(hTIndustrySalonApply);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-通过id删除")
	@ApiOperation(value = "ht_industry_salon_apply-通过id删除", notes = "ht_industry_salon_apply-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		hTIndustrySalonApplyService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-批量删除")
	@ApiOperation(value = "ht_industry_salon_apply-批量删除", notes = "ht_industry_salon_apply-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.hTIndustrySalonApplyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_industry_salon_apply-通过id查询")
	@ApiOperation(value = "ht_industry_salon_apply-通过id查询", notes = "ht_industry_salon_apply-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		HTIndustrySalonApply hTIndustrySalonApply = hTIndustrySalonApplyService.getById(id);
		if (hTIndustrySalonApply == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hTIndustrySalonApply);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param hTIndustrySalonApply
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HTIndustrySalonApply hTIndustrySalonApply) {
		return super.exportXls(request, hTIndustrySalonApply, HTIndustrySalonApply.class, "ht_industry_salon_apply");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, HTIndustrySalonApply.class);
	}

}
